<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>更改密码</span>
    </div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="原始密码" prop="prePassword">
        <el-input
          type="password"
          v-model="ruleForm.prePassword"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="enterPassword">
        <el-input
          type="password"
          v-model="ruleForm.enterPassword"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updateUserPassword } from '@/req/userAjax'
import { alterError, alterSuccess } from '@/util/msgTips'
import store from '@/util/storeManage'
import {
  validatePrePassword,
  validateEnterPassword,
  validateNewPassword
} from '@/util/rule/fromRule'
export default {
  data () {
    return {
      ruleForm: {
        prePassword: '',
        password: '',
        enterPassword: ''
      },
      rules: {
        prePassword: [{ validator: validatePrePassword, trigger: 'blur' }],
        password: [{ validator: validateNewPassword(this), trigger: 'blur' }],
        enterPassword: [
          { validator: validateEnterPassword(this), trigger: 'blur' }
        ]
      },
      self: this
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 修改密码
          const { code, msg } = await updateUserPassword(
            this.ruleForm.enterPassword
          )
          if (code === 0) {
            this.ruleForm = {}
            alterSuccess(msg)
          } else {
            alterError(msg)
          }
          // 清除token
          store.del('t_k')
          // 跳转到登入页面
          setTimeout(() => {
            this.$router.go(0)
          }, 1000)
        } else {
          alterError('请输入完整')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 400px;
}

.el-form-item--small .el-form-item__error {
  margin-top: 2px;
}

.el-form-item__error {
  position: absolute !important;
  top: 16px;
  text-align: center;
  background-color: red;
  filter: alpha(Opacity=40);
  opacity: 0.4;
  width: 120px;
  height: 21px;
  border-radius: 2px;
  font-size: 12px;
  color: #fff;
  line-height: 21px;
  z-index: 99;
}
</style>
